package com.wesley.activemq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @author:wesley
 */
public class ProductTest {

    /**
     * 这是通过点对点的方式，queue直接消费
     *
     * @throws JMSException
     */
    private void testSendMessageByQueue() throws JMSException {
        //tcp连接
        String url = "tcp://localhost:61616";
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        Connection connection = connectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue("test-queue");
        MessageProducer messageProducer = session.createProducer(queue);
        TextMessage textMessage = session.createTextMessage("hello ~  test-queue1");

        messageProducer.send(textMessage);
        messageProducer.close();
        session.close();
        connection.close();
    }

    private void testSendMessageByTopic() throws JMSException {
        //tcp连接
        String url = "tcp://localhost:61616";
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        Connection connection = connectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic("test-topic");
        MessageProducer messageProducer = session.createProducer(topic);
        TextMessage textMessage = session.createTextMessage("hello ~  test-topic");

        messageProducer.send(textMessage);
        messageProducer.close();
        session.close();
        connection.close();
    }



    public static void main(String[] args) throws JMSException {
        //发送信息
//        new ProductTest().testSendMessageByQueue();
        new ProductTest().testSendMessageByTopic();
    }



}
